package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.StrategyInvestPresenter;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.SpApplication;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StrategyBean;
import com.sskj.lib.bean.UsableBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * @author Hey
 * Create at  2019/07/10
 */
@Route(path = RConfig.FUNCTIONS_STRATEGYDETAILS)
public class StrategyInvestActivity extends BaseActivity<StrategyInvestPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvName)
    TextView tvName;
    @BindView(R2.id.tvKetou)
    TextView tvKetou;
    @BindView(R2.id.tvNumber)
    EditText tvNumber;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.tvYue)
    TextView tvYue;
    @BindView(R2.id.tvShouyi)
    TextView tvShouyi;
    @BindView(R2.id.cbTrue)
    CheckBox cbTrue;
    @BindView(R2.id.register_ok)
    TextView registerOk;
    @BindView(R2.id.tvXieyi)
    TextView tvXieyi;
    @BindView(R2.id.btStart)
    Button btStart;
    @Autowired
    public StrategyBean.List1Bean List1Bean;
    private String pay_type = "usdt";
    private int page = 1;
    private String Id;
    private String jishu;
    private String languages = "1";

    //    pay_type=usdt&languages=1&type=2&page=1
    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_strategy_invest;
    }

    @Override
    public StrategyInvestPresenter getPresenter() {
        return new StrategyInvestPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_investActivity1));
        tvShouyi.setText(CommonUtil.dealCoinNum("0", List1Bean.getPay_type().toUpperCase()) + List1Bean.getPay_type().toUpperCase());
        mPresenter.getBorrowData(pay_type, page + "", "2", languages, true);
        tvNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(5)});

        ClickUtil.click(300, tvXieyi, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_AGREEMENT).navigation();
        });
        ClickUtil.click(300, btStart, () -> {

            if (isEmpty(getText(tvNumber))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity2));
                return;
            }
            if (!cbTrue.isChecked()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity33));
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(BaseHttpConfig.HETONG);
            stringBuilder.append("?a=");
            stringBuilder.append(SPUtil.get(SPConfig.ACCOUNT, ""));
            stringBuilder.append("&t=");
            stringBuilder.append(SPUtil.get(SPConfig.TOKEN, ""));
            stringBuilder.append("&i=");
            stringBuilder.append(Id);
            stringBuilder.append("&l=");
            stringBuilder.append("cn");
            stringBuilder.append("&c=");
            stringBuilder.append(getText(tvNumber));

            ARouter.getInstance().build(RConfig.APP_ACTIVITY_ZHUANPAN)
                    .withString(Constans.TITLE, App.INSTANCE.getString(R.string.functions_functionsHetong))
                    .withString(Constans.WEB_URL, stringBuilder.toString())
                    .navigation(this, Constans.HETONG);

        });
    }

    private void updateUI(StrategyBean.List1Bean list1Bean) {
        Id = list1Bean.getId();
        tvName.setText(list1Bean.getName());
        tvKetou.setText(CommonUtil.dealCoinNum(list1Bean.getSurplus(), list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
        tvNumber.setHint(String.format(App.INSTANCE.getString(R.string.functions_strategyInvestActivity5), NumberUtil.keepNoZero(list1Bean.getShare_min())));
        tvType.setText(list1Bean.getPay_type().toUpperCase());
        String yearIncome = ArithUtil.txfloat(Double.parseDouble(list1Bean.getInterest_rate()), 100);
        try {
            double dayIncome = ArithUtil.div(Double.parseDouble(yearIncome), 365, 8);
            jishu = String.valueOf(ArithUtil.mul(dayIncome, Double.valueOf(list1Bean.getCycle())));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        tvNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isEmpty(s.toString())) {
                    tvShouyi.setText(CommonUtil.dealCoinNum("0", list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
                    return;
                }
                if (isEmpty(jishu)) {
                    return;
                }
                tvShouyi.setText(
                        CommonUtil.dealCoinNum
                                (NumberUtil.keepNoZero
                                                (ArithUtil.mul(Double.parseDouble(s.toString()), Double.valueOf(jishu)))
                                        , list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
            }
        });

    }

    @Override
    public void initData() {
        mPresenter.getUsable(List1Bean.getId());
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, StrategyInvestActivity.class);
        context.startActivity(intent);
    }

    public void InvestSuccess() {
        ARouter.getInstance().build(RConfig.FUNCTIONS_JBSX).navigation();
        ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity4));
        finish();
    }

    @SuppressLint("SetTextI18n")
    public void getStrategyDataSuccess(StrategyBean data) {
        if (data == null) {
            return;
        }
        Observable.fromIterable(data.getList1())
                .filter(list1Bean -> list1Bean.getId().equals(List1Bean.getId()))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(list1Bean -> {
//                    tvYue.setText(App.INSTANCE.getString(R.string.functions_investActivity6) + data.getPrice() + "" + list1Bean.getPay_type().toUpperCase());
//                    tvYue.setText(App.INSTANCE.getString(R.string.functions_investActivity6) + CommonUtil.dealCoinNum(data.getPrice(), list1Bean.getPay_type().toUpperCase()) + "" + list1Bean.getPay_type().toUpperCase());
                    updateUI(list1Bean);
                });
    }

    public void getUsableOK(UsableBean data) {
        if (data == null) {
            return;
        }
        tvYue.setText(App.INSTANCE.getString(R.string.functions_investActivity6) + CommonUtil.dealCoinNum(data.getPrice(), data.getType().toUpperCase()) + data.getType().toUpperCase());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constans.HETONG && resultCode == RESULT_OK) {
            BottomDialog.posswordInput(this, tv -> {
                mPresenter.startInvest(getText(tvNumber), Id, tv);
            });
        }
    }
}
